<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="card">
        <el-input
          v-model="queryParams.card"
          placeholder="请输入身份证号"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名称" prop="drugName">
        <el-input
          v-model="queryParams.drugName"
          placeholder="请输入药品名称"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟进状态" prop="recover">
        <el-select
          v-model="queryParams.recover"
          placeholder="请选择状态"
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="dict in dict.type.yy_datacenter_reply"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
          style="width: 160px"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dataCenter:DrugApply:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dataCenter:DrugApply:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dataCenter:DrugApply:remove']"
          >删除
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dataCenter:DrugApply:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="DrugApplyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column
        label="身份证号"
        align="center"
        prop="card"
        width="180"
      />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="药品名称" align="center" prop="drugName" />
      <el-table-column label="药品数量" align="center" prop="drugNum" />
      <el-table-column label="图片" align="center" width="190" prop="picture">
        <template slot-scope="scope" v-if="scope.row.picture">
          <!-- <el-image
            v-for="(image, index) in scope.row.picture.split(',')"
            :key="index"
            :src="baseUrl + image" 
            :preview-src-list="scope.row.picture.split(',')"
            style="margin-right: 5px;width: 50px;height: 50px;"
          ></el-image> -->
          <image-preview
            :src="image"
            style="margin-right: 5px"
            :width="50"
            :height="50"
            :key="index"
            v-for="(image, index) in scope.row.picture.split(',')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        class-name="cell-ellipsis"
        ><template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p style="width: 300px">{{ scope.row.remark }}</p>
            <div slot="reference" class="name-wrapper">
              {{ scope.row.remark }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="跟进状态" align="center" prop="recover">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_datacenter_reply"
            :value="scope.row.recover"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="跟进备注"
        align="center"
        prop="recoverRemark"
        class-name="cell-ellipsis"
        ><template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p style="width: 300px">{{ scope.row.recoverRemark }}</p>
            <div slot="reference" class="name-wrapper">
              {{ scope.row.recoverRemark }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建人" align="center" prop="createBy" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleStatus(scope.row)"
            v-hasPermi="['dataCenter:DrugApply:edit']"
            >跟进
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataCenter:DrugApply:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataCenter:DrugApply:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据中台-患者药品申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-row>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入姓名"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input
                v-model="form.phonenumber"
                placeholder="请输入手机号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="card">
              <el-input
                v-model="form.card"
                placeholder="请输入身份证号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品名称" prop="drugName">
              <el-input
                v-model="form.drugName"
                placeholder="请输入药品名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品数量" prop="drugNum">
              <el-input
                v-model="form.drugNum"
                placeholder="请输入药品数量"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-form-item label="药品图片" prop="picture">
            <image-upload v-model="form.picture" :limit="6" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              placeholder="请输入备注"
              clearable
            />
          </el-form-item>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改数据中台-患者药品申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="reply"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="top"
        label-width="80px"
      >
        <!-- <el-form-item label="是否回复" prop="recover">
          <el-input v-model="form.recover" placeholder="请输入是否回复" />
        </el-form-item> -->
        <el-form-item label="" prop="recoverRemark">
          <el-input
            type="textarea"
            :rows="6"
            v-model="form.recoverRemark"
            placeholder="请输入备注(选填)"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDrugApply,
  getDrugApply,
  delDrugApply,
  addDrugApply,
  updateDrugApply,
} from "@/api/dataCenter/DrugApply";

export default {
  name: "DrugApply",
  dicts: ["sys_user_sex", "yy_datacenter_reply"],
  data() {
    return {
      baseUrl: process.env.VUE_APP_IMG_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据中台-患者药品申请表格数据
      DrugApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      reply: false, //回复弹窗
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phonenumber: null,
        card: null,
        drugName: null,
        createTime: null,
        recover: null,
      }, // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "姓名不能为空",
            trigger: "blur",
          },
        ],
        phonenumber: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "blur",
          },
        ],
        card: [
          {
            required: true,
            message: "身份证号不能为空",
            trigger: "blur",
          },
        ],
        sex: [
          {
            required: true,
            message: "性别不能为空",
            trigger: "change",
          },
        ],
        drugName: [
          {
            required: true,
            message: "药品名称不能为空",
            trigger: "blur",
          },
        ],
        drugNum: [
          {
            required: true,
            message: "药品数量不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据中台-患者药品申请列表 */
    getList() {
      this.loading = true;
      listDrugApply(this.queryParams).then((response) => {
        this.DrugApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reply = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phonenumber: null,
        card: null,
        sex: null,
        drugName: null,
        drugNum: null,
        remark: null,
        recover: null,
        picture: null,
        recoverRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDrugApply(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品申请";
      });
    },
    // 回复申请
    handleStatus(row) {
      this.reset();
      const id = row.id || this.ids;
      getDrugApply(id).then((response) => {
        response.data.recover = "1";
        this.form = response.data;
        this.reply = true;
        this.title = "跟进备注";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   this.form.display = this.form.display.join(",");
          if (this.form.id != null) {
            updateDrugApply(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.reply = false;
              this.getList();
            });
          } else {
            addDrugApply(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该申请？")
        .then(function () {
          return delDrugApply(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dataCenter/DrugApply/export",
        {
          ...this.queryParams,
        },
        `DrugApply_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
/* 使用CSS实现单行文本超出省略 */
.cell-ellipsis .cell {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  max-width: 100%;
}
</style>
